package jp.co.htkk.mapper;

import jp.co.htkk.dto.${TableName!}Dto;
import jp.co.htkk.entity.${TableName!};
import org.apache.ibatis.annotations.*;

@Mapper
public interface ${TableName!}Mapper {

    /**
     * @param ${tableName!}
     * @return number of ${tableName!} insert
     */
    @Options(useGeneratedKeys = true, keyProperty = "${keyName!}", keyColumn = "${key_name!}")
    @Insert({"INSERT INTO ${table_name!}(<#list  columns as field> ${field.column_name!}<#if field_has_next>,</#if> </#list>) VALUES ",
             "(<#list  columns as field> ${r"#{"}${field.columnName!}${r"}"}<#if field_has_next>,</#if> </#list>)"}
    )
    int create${TableName!}(${TableName!} ${tableName!});

    /**
     * @param id
     * @return ${TableName!}Dto
     */
    @Select({"SELECT <#list  columns as field> ${field.column_name!}<#if field_has_next>,</#if> </#list>",
            "FROM ${table_name!} " ,
            "<where>",
                " AND${key_name!} = ${r"#{id}"}",
                " AND delete_flag!=1",
            "</where>",
            "WHERE "})
    ${TableName!}Dto get${TableName!}ById(String id);
    @Delete("UPDATE ${table_name!} SET delete_flag = 1 WHERE ${key_name!} = ${r"#{id}"}")
    int delete${TableName!}ById(String id);
    /**
    * @param ${tableName!}Dto
    * @return ${TableName!}Dto
    */
    @Update({"<script> " ,
            "UPDATE ${table_name!}" +
            "        <trim prefix='SET' suffixOverrides=','>" ,
<#list  columns as field>
            <#if field.isPk=="1">
            <#else>
            "            <if test='${field.columnName!} != null'>${field.column_name!} = ${r"#{"}${field.columnName!}${r"}"},</if>" ,
            </#if>
</#list>
            "        </trim>\n" ,
            "        WHERE ${key_name!} =${r"#{id}"} AND delete_flag!=1" ,
            "</script>"})
    int update${TableName!}(${TableName!}Dto ${tableName!}Dto);

    @Delete({
            "    update ${table_name!} set delete_flag=1" ,
                    "    <where>" ,
                    "        <foreach collection='list' item='item' separator=',' open='(' close=')'>" ,
                    "            ${r"#{item}"}" ,
                    "        </foreach>" ,
                    "    </where>"
    })
    int bulkDelete(List<${KeyJavaType!}> idList);


    @Update({"<script>",
        "UPDATE ${table_name!}",
            " SET",
            <#list  columns_remove_ingore as field>
            <#if field.isPk=="1">
            <#else>
            "    ${field.column_name!} =ELT((FIELD(id,",
            "    <foreach collection='list' item='item' separator=','>",
            "        ${r'#{item.'}${keyName!}${r'}'}",
            "    </foreach>",
            "    )),",
            "    <foreach collection='list' item='item' separator=','>",
            "        ${r'#{item.'}${field.columnName!}${r'}'}",
            "    </foreach>",
            "    )<#if field_has_next>,</#if>",

            </#if>
            </#list>
            " <where>",
            "     id in",
            "     <foreach collection='list' item='item' separator=',' open='(' close=')'>",
            "         ${r'#{item.'}${keyName!}${r'}'}",
            "     </foreach>",
            " </where>",
            "</script>"})
    int bulkUpdate(List<${TableName!}Dto> toothStatusList);

}
